package mis.ds.ps.model.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import mis.ds.ps.model.dao.MappercompostoDao;
import mis.ds.ps.model.obj.Mappercomposto;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.stereotype.Repository;

import com.felees.hbnpojogen.persistence.impl.GenericHibernateDaoImpl;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * DAO for table: Mappercomposto.
 * 
 * @author autogenerated
 */
@Repository
public class MappercompostoDaoImpl extends GenericHibernateDaoImpl<Mappercomposto, Long> implements MappercompostoDao {

	private static Logger logger = Logger.getLogger(DisciplinasDaoImpl.class);

	public static final int ID = 1;
	public static final int NOME = 2;

	private Connection mysqlConn;

	/** Constructor method. */
	public MappercompostoDaoImpl() {
		super(Mappercomposto.class);

		PropertyConfigurator.configure("log4j.properties");
	}

	public List<Mappercomposto> getMappersComp() {

		List<Mappercomposto> mapCompList = new ArrayList<Mappercomposto>();

		String sql = "SELECT * from mappercomposto";
		PreparedStatement prest;
		try {

			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
			logger.debug("SQL select statement [" + prest.toString() + "]");
			ResultSet rs = prest.executeQuery();
			while (rs.next()) {
				Mappercomposto mc = new Mappercomposto(Long.valueOf(rs.getString(ID)));
				mc.setNome(rs.getString(NOME));

				mapCompList.add(mc);
			}

		} catch (SQLException e1) {
			logger.debug("Cannot get composed mappers from DB ", e1);
		}
		return mapCompList;

	}

	public int insertMapperComposite(Mappercomposto compostoMapper) {

		int totalInsert = 0;
		String sql = "INSERT mappercomposto(NOME) VALUES(?)";
		PreparedStatement prest;
		try {
			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
			prest.setString(1, compostoMapper.getNome());
			logger.debug("CompMapper SQL insert statement " + prest.toString());
			int count = prest.executeUpdate();
			totalInsert += count;
			logger.debug("Insert " + totalInsert + " compMappers");
		} catch (SQLException e1) {
			logger.debug("Cannot insert compMapper on DB ", e1);
		}

		return totalInsert;
	}

	public int selectIDFromMapperComposite(String name) {

		int mapperIndex = 0;

		String sql = "SELECT ID from mappercomposto where NOME like '" + name + "'";
		PreparedStatement prest;
		try {

			prest = (PreparedStatement) mysqlConn.prepareStatement(sql);
			logger.debug("SQL select statement [" + prest.toString() + "]");
			ResultSet rs = prest.executeQuery();
			while (rs.next()) {
				mapperIndex = Integer.valueOf(rs.getString(ID));
			}

		} catch (SQLException e1) {
			logger.debug("Cannot get id from mapper name " + name + " " + e1.getMessage());
		}

		return mapperIndex;
	}

	public Connection getMysqlConn() {
		return mysqlConn;
	}

	public void setMysqlConn(Connection mysqlConn) {
		this.mysqlConn = mysqlConn;
	}
}
